home *** CD-ROM | disk | FTP | other *** search
/ NetNews Offline 2 / NetNews Offline Volume 2.iso / news / comp / lang / c-part2 / 13335 < prev    next >
Encoding:
Internet Message Format  |  1996-08-05  |  1.9 KB

  1. Path: erich.triumf.ca!bennett
  2. From: bennett@erich.triumf.ca (P.Bennett)
  3. Newsgroups: comp.lang.c
  4. Subject: Re: File Open Problem - Please provide clues
  5. Date: 6 Apr 1996 09:20 PST
  6. Organization: TRIUMF: Tri-University Meson Facility
  7. Distribution: world
  8. Message-ID: <6APR199609203190@erich.triumf.ca>
  9. References: <4jkq8h$1it6@useneta1.news.prodigy.com> <xZaQmMD12aUz1@andi.art-line.de>
  10. NNTP-Posting-Host: ftp.triumf.ca
  11. News-Software: VAX/VMS VNEWS 1.50    
  12.  
  13. In article <xZaQmMD12aUz1@andi.art-line.de>, andi@art-line.de (Andreas Winkelmann) writes...
  14. >Hi Steve.
  15. >> Hi I'm having problems with opening files which are named   
  16. >> after simple numbers:                                       
  17.  
  18. >> I get mismatched type errors when using FOPEN:              
  19. >>  int num;                                                   
  20. >>  filepointer = FOPEN(num,"r");            or                           
  21. >>  filepointer = FOPEN("num","r");
  22. >fopen() accepts as the first argument only a stringpointer. You
  23. >have to convert the number in a string.
  24. >char buffer[21];
  25. >FILE *fp;
  26. >int num;
  27. >sprintf( (char*)&buffer,"%ld.txt",num);
  28. >fp = fopen( (char*)&buffer, "r" );
  29.  
  30. What is the (char*) cast for?  'buffer' is already a char*.  The name of an
  31. array acts as a pointer to the first element when used as a function argument.
  32.  
  33. Also, "%ld" is used to print longs - you use "%d" to print an int.
  34.  
  35. So the above should be:
  36.     sprintf(buffer, "%d.txt", num);
  37.     fp = fopen(buffer, "r");
  38.  
  39. Peter Bennett VE7CEI                | Vessels shall be deemed to be in sight
  40. Internet: bennett@triumf.ca         | of one another only when one can be
  41. Packet: ve7cei@ve7kit.#vanc.bc.ca   | observed visually from the other
  42. TRIUMF, Vancouver, B.C., Canada     |                          ColRegs 3(k)
  43. GPS and NMEA info and programs: ftp://sundae.triumf.ca/pub/peter/index.html
  44. or: ftp://ftp-i2.informatik.rwth-aachen.de/pub/arnd/GPS/peter/index.html
  45.  
  46.  
  47.  
  48.  
  49.  
  50.  
  51.  
  52.  
  53.